﻿using System;
using System.Collections.Generic;
using LitJson;
using LogSystem;
using UnityEngine;
using UnityEngine.Rendering;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.Liberty
{
    public class LibertyAnimationClipManager : Singleton<LibertyAnimationClipManager>
    {
        // 事件动画路径
        private const string StreetEventAnimationName = "Assets/ResourcesAssets/Hanging/Animations/{0}.anim";
        
        // 当前动画列表
        private Dictionary<string, AnimationClip> _animationClips = new Dictionary<string, AnimationClip>();
        
        private ResLoader _resLoader;

        
    #region 街区事件

        /// <summary>
        /// 将街区事件的动画名转为完整的资源路径
        /// </summary>
        /// <param name="animationName"></param>
        /// <returns></returns>
        public string ConvertStreetEventAnimationName2Path(string animationName)
        {
            return string.Format(StreetEventAnimationName, animationName);
        }
    
        /// <summary>
        /// 加载街区事件动画资源 (传动画名，动画需要放在指定路径下：Assets/ResourcesAssets/Hanging/Animations/{0}.anim)
        /// </summary>
        /// <param name="animationNames"></param>
        /// <param name="callback"></param>
        public void PreloadStreetEventAnimationClips(List<string> animationNames, Action callback)
        {
            List<string> paths = ListPool<string>.Get();
            paths.Clear();

            foreach (var animationName in animationNames)
            {
                paths.Add(ConvertStreetEventAnimationName2Path(animationName));
            }
            PreloadAnimationClips(paths, callback);
            
            ListPool<string>.Release(paths);
        }

    #endregion
        

    
        /// <summary>
        /// 预加载动画资源 (需使用完整资源路径)
        /// </summary>
        /// <param name="paths"></param>
        /// <param name="callback"></param>
        public void PreloadAnimationClips(List<string> paths, Action callback)
        {
            if (_resLoader == null)
            {
                _resLoader = ResLoader.Alloc();
            }

            int loadCount = 0;
            if (paths != null)
            {
                foreach (var path in paths)
                {
                    if (!_animationClips.ContainsKey(path))
                    {
                        loadCount++;
                        _resLoader.Add2Load(path, (success, name, asset) => {
                            if (success)
                            {
                                var clip = asset as AnimationClip;
                                if (clip)
                                {
                                    _animationClips[path] = clip;
                                }
                                else
                                {
                                    Debug.LogError(LogModule.LibertySceneBattle, $"加载AnimationClip失败 : {path} ");
                                }
                            }
                            else
                            {
                                Debug.LogError(LogModule.LibertySceneBattle, "load Asset failed : " + name);
                            }
                        });
                    }
                }
            }

            if (loadCount > 0)
            {
                _resLoader.Load(() =>
                {
                    callback?.Invoke();
                });
            }
            else
            {
                callback?.Invoke();
            }
        }

        /// <summary>
        /// 加载AnimationClip (需使用完整资源路径)
        /// </summary>
        /// <param name="path"></param>
        /// <param name="callback"></param>
        public void LoadAnimationClip(string path, Action<AnimationClip> callback)
        {
            if (_resLoader == null)
            {
                _resLoader = ResLoader.Alloc();
            }
            
            AnimationClip animationClip;
            if (_animationClips.TryGetValue(path, out animationClip))
            {
                callback?.Invoke(animationClip);
            }
            else
            {
                _resLoader.Add2Load(path, (success, name, asset) => {
                    if (success)
                    {
                        var clip = asset as AnimationClip;
                        if (clip)
                        {
                            _animationClips[path] = clip;
                            callback?.Invoke(clip);
                        }
                        else
                        {
                            Debug.LogError(LogModule.LibertySceneBattle, $"加载AnimationClip失败 : {path} ");
                        }
                    }
                    else
                    {
                        Debug.LogError(LogModule.LibertySceneBattle, "load Asset failed : " + name);
                    }
                });
                _resLoader.Load();
            }
        }
        
        /// <summary>
        /// 卸载资源
        /// </summary>
        public void ClearAll()
        {
            if (_resLoader != null)
            {
                _resLoader.Recycle2Cache();
                _resLoader = null;
            }
            _animationClips.Clear();
        }
        
        
    }
}
